package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	@Override
	public List<JSONObject> getBaseCategoryList() {
		//声明一个集合保存数据
		List<JSONObject> categoryList = new ArrayList<>();
		//查询所有数据 select * from base_category_view;
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
		//baseCategoryViewList使用stream流进行分组，分组key为category1Id
		Map<Long, List<BaseCategoryView>> category1Map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		/*复制小口号，写死右箭头，落地大括号！
			Map<Long, List<BaseCategoryView>> longListMap = baseCategoryViewList.stream().collect(Collectors.groupingBy(baseCategoryView -> {
				return baseCategoryView.getCategory1Id();
			}));*/
		//遍历category1Map集合
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = category1Map.entrySet().iterator();
		while (iterator.hasNext()){
			//获取entry对象
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			//获取key
			Long category1Id = entry.getKey();
			List<BaseCategoryView> baseCategoryViewList1 = entry.getValue();

			//创建一级分类对象
			JSONObject category1 = new JSONObject();
			category1.put("categoryId",category1Id);
			category1.put("categoryName",baseCategoryViewList1.get(0).getCategory1Name());
			//获取二级分类数据
			Map<Long, List<BaseCategoryView>> categoryMap = baseCategoryViewList1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			//遍历category2Map集合
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = categoryMap.entrySet().iterator();
			//创建一个二级分类对象
			ArrayList<JSONObject> category2ChildList = new ArrayList<>();
			while (iterator1.hasNext()){
				//获取二级分类对象
				Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
				Long category2Id = entry1.getKey();
				List<BaseCategoryView> baseCategoryViewList2 = entry1.getValue();
				//创建二级分类对象
				JSONObject category2 = new JSONObject();
				category2.put("categoryId",category2Id);
				category2.put("categoryName",baseCategoryViewList2.get(0).getCategory2Name());
				//获取到三级分类集合并放入对象中
				List<JSONObject> category3List = baseCategoryViewList2.stream().map(baseCategoryView -> {
					//	创建三级分类对象
					JSONObject category3 = new JSONObject();
					category3.put("categoryId", baseCategoryView.getCategory3Id());
					category3.put("categoryName", baseCategoryView.getCategory3Name());
					return category3;
				}).collect(Collectors.toList());
				//  将三级分类数据添加到二级分类数据中
				category2.put("categoryChild", category3List);
				category2ChildList.add(category2);
			}
			category1.put("categoryChild",category2ChildList);
			//将一级分类数据放入categoryList集合中
			categoryList.add(category1);
		}
		return categoryList;
	}

	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		//多表关联查询
		return baseAttributeMapper.selectAttributeList(category1Id);
	}
	@Override
	public JSONObject getBaseCategoryList(Long category1Id) {
		//  声明一个一级分类对象
		JSONObject category1 = new JSONObject();
		//  需要根据一级分类Id查询 base_category_view
		//  select * from base_category_view where category1_id=1
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));
		//  赋值：
		category1.put("categoryId",category1Id);
		category1.put("categoryName",baseCategoryViewList.get(0).getCategory1Name());
		//  存储的是二级分类集合;
		//  从一级分类集合中，按照二级分类Id进行分组！
		Map<Long, List<BaseCategoryView>> map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = map.entrySet().iterator();
		//  声明一个集合来存储二级分类集合
		ArrayList<JSONObject> category2ChildList = new ArrayList<>();
		//  判断是否有下一个数据
		while (iterator.hasNext()){
			//  获取到这个对象;
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			//  获取到二级分类Id
			Long category2Id = entry.getKey();
			List<BaseCategoryView> baseCategoryViewList2 = entry.getValue();
			//  创建二级分类对象
			JSONObject category2 = new JSONObject();
			category2.put("categoryId",category2Id);
			category2.put("categoryName",baseCategoryViewList2.get(0).getCategory2Name());
			//  存储三级分类集合
			List<JSONObject> category3ChildList = baseCategoryViewList2.stream().map(baseCategoryView -> {
				JSONObject category3 = new JSONObject();
				category3.put("categoryId", baseCategoryView.getCategory3Id());
				category3.put("categoryName", baseCategoryView.getCategory3Name());
				return category3;
			}).collect(Collectors.toList());
			//  将三级级分类对象添加二级对象中
			category2.put("categoryChild",category3ChildList);
			//  将二级分类对象添加到集合中
			category2ChildList.add(category2);

		}
		//  将二级分类集合添加到一级分类中
		category1.put("categoryChild",category2ChildList);
		//  返回数据
		return category1;
	}

	@Override
	public List<BaseCategory3> getBaseCategory3List(Long category1Id) {
		List<BaseCategory2> baseCategory2List= baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id));
		//  需要baseCategory2List 从这个集合中获取二级分类Id集合
		List<Long> baseCategory2IdList = baseCategory2List.stream().map(BaseCategory2::getId).collect(Collectors.toList());
		List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>().in(BaseCategory3::getCategory2Id, baseCategory2IdList).eq(BaseCategory3::getIsTop, 1).last(" limit 7"));
		return baseCategory3List;
	}
	@Override
	public List<BaseCategory1> findAllCategory1() {
		//  查询所有数据
		return baseCategory1Mapper.selectList(null);
	}
}
